/*
 * Edit Amenities Module JS
 * This creates the admin GUI Edit Page for the Unit Amenities Module
 *
 * JSLint.com Check:
 */

RM.Pages.Functions.Amenities_Unit_Save = function(){
    RM.Pages.Amenities_Unit_Edit_Form.getForm().submit({
        success: function(form, action) {

        },
        waitMsg: RM.Translate.Common.Saving,
        waitTitle: RM.Translate.Common.PleaseWait
    });
};

RM.Pages.Amenities_Unit_Edit_Toolbar = {
    xtype : "panel",
    id : "rm_pages_amenities_unit_edit_toolbar",
    bodyBorder : false,
    html : RM.Common.getToolbar([{image: RM.BaseLargeImageURL+"save.gif", label: RM.Translate.Common.Save, link: "RM.Pages.Functions.Amenities_Unit_Save()"}])
};
RM.Toolbars.push(RM.Pages.Amenities_Unit_Edit_Toolbar);

RM.Pages.Amenities_Unit_Edit_Fieldset = new Ext.form.FieldSet({
    id: "rm_pages_amenities_feed_fieldset",
    title: RM.Translate.Admin.Amenities.Unit.Selected,
    autoHeight: true,
    layout: {
        type: 'table',
        columns: 4
    },
    bodyBorder : false,
    defaults: {width: 300},
    labelWidth: 190
});

// load the values
RM.Pages.Functions.Amenities_Unit_EditJson_Request = function(){

    var unit_id = Ext.getCmp('edit_unit[id]').getValue();
    Ext.getCmp('rm_amenities_edit_unit_id').setValue(Ext.getCmp('edit_unit[id]').getValue());

    Ext.getCmp("rm_pages_amenities_feed_fieldset").removeAll(true);

    var myMask = new Ext.LoadMask('content-panel', {msg:RM.Translate.Common.PleaseWait});
    myMask.show();

    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller : 'Amenities',
            action: 'getamenitiesjson',
            parameters : [{
                name : 'unit_id',
                value : unit_id
            }]
        }),
        method: 'POST',
        success: function(responseObject) {
            var jsonObject = Ext.util.JSON.decode(responseObject.responseText);
            var data = jsonObject.data;
            var selection = jsonObject.selection;
            
            var iconURL = '';var rowIndex;var checkedVar;var FormItems = [];

            var i = 0;for (i; i < data.length; i++ ){

                checkedVar = false;
                if (selection !== null && RM.Common.InArray(data[i].id, selection)){
                    checkedVar = true;
                }

                iconURL = RM.RootURL + "RM/userdata/images/Amenities/" + data[i].icon_url;

                FormItems.push(new Ext.Panel({
                    width: 40,
                    autoHeight: true,
                    layout: {
                        type: 'table',
                        columns: 2
                    },
                    items:[
                        new Ext.Panel({
                            id: "rm_pages_amenities_edit_unit_selection_icon_" + data[i].id,
                            html: "<img src='" + iconURL + "' alt='' height='30'><br/><div class='rm-module-amenities-amenity-cell-inner-textlabel'>" + data[i].name + "</div>",
                            width: 40,
                            autoHeight: true,
                            border: false
                        }),
                        new Ext.ux.form.XCheckbox({
                            id: "rm_pages_amenities_edit_unit_selection_checkbox[" + data[i].id + "]",
                            name : "rm_pages_amenities_edit_unit_selection_checkbox[" + data[i].id + "]",
                            inputValue : 1,
                            checked: checkedVar
                        })
                    ]
                }));
            }

            Ext.getCmp("rm_pages_amenities_feed_fieldset").add(FormItems);
            Ext.getCmp("rm_pages_amenities_feed_fieldset").doLayout();
            
            myMask.hide();
        },
        failure: function(){
            myMask.hide();
        }
    };
    conn.request(request);
};

RM.Pages.Amenities_Unit_Edit_Form = new Ext.FormPanel({
    id : "rm_pages_amenities_unit_edit_form",
    title: RM.Translate.Admin.Amenities.Unit.Title,
    xtype : "form",
    height: 600,
    autoScroll: true,
    url : RM.Common.AssembleURL({
        controller: 'Amenities',
        action: 'updateunitJson'
    }),
    frame : true,
    items : [
        {
            xtype: "hidden",
            id: 'rm_amenities_edit_unit_id'
        },
        RM.Pages.Amenities_Unit_Edit_Fieldset,
    ],
    listeners: {
        'beforehide' : function(){
            Ext.getCmp('toolbar_panel').layout.setActiveItem('rm_pages_units_edit_toolbar');
            return true;
        },
        'beforeshow' : function(){
            Ext.getCmp('toolbar_panel').layout.setActiveItem('rm_pages_amenities_unit_edit_toolbar');
            RM.Pages.Functions.Amenities_Unit_EditJson_Request();
            return true;
        }
    }
});

RM.Pages.Units_Edit_TabPanel.add(RM.Pages.Amenities_Unit_Edit_Form);
